package com.tensquare.web.fiter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("web正在执行过滤器，，，，，，，，，，");
        //1. 得到requestcontext对象，目的是为了的request对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        //2. 得到请求对象
        HttpServletRequest request = currentContext.getRequest();
        //3. 得到请求头
        String authorization = request.getHeader("Authorization");
        //4. 如果 请求头不为空，就将请求头加到网关zuul的请求头上
        if (authorization != null ){
            currentContext.addZuulRequestHeader("Authorization",authorization);
        }
        return null;
    }
}
